<?php

namespace App\Models;

use App\Dengje\Utils\Settings;
use App\Models\Posts\WxLike;
use App\Models\Posts\WxPost;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use function Laminas\Stratigility\host;


class WxUser extends Model
{
	use HasDateTimeFormatter;
    use SoftDeletes;

    protected $appends = ['follow_count','follow_user_count','like_count'];

    protected $table = 'wx_user';

    /**
     * openId: "oxLaa1N3zIwJ8z4qM7juTsf3yOpc", nickName: "爱喝啤酒的友", gender: 1, language: "zh_CN",…}
    avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTL0ian6uh42actKNBGAKfyBDyObibQBZuS5VEPzMZic59gksRkRZdM0gb5JpBxgMEHPMEPVhe3QWCHnw/132"
    city: "Shenzhen"
    country: "China"
    gender: 1
    language: "zh_CN"
    nickName: "爱喝啤酒的友"
    openId: "oxLaa1N3zIwJ8z4qM7juTsf3yOpc"
    province: "Guangdong"
    watermark: {timestamp: 1613808334, appid: "wx531a5badcfa272db"}
     * @param $data
     * @return bool
     */
    public function registerUser($data)
    {
        $openid = $data['openId'];
        // 判断是否已经注册过
        if(WxUser::where('weixin_openid',$openid)->count()>0){
           return true;
        }
//        $maxId = WxUser::max('id');
        $user = new WxUser();
        $user->weixin_openid=$data['openId'];
        $user->user_name=$data['nickName'];
        $user->weixin_name=$data['nickName'];
        $user->user_background_maps = Settings::get('user_background_maps');
        $user->user_avatar=$data['avatarUrl'];
        $user->country=$data['country'];
        $user->province=$data['province'];
        $user->city=$data['city'];
        $user->gender=$data['gender'];
        $user->save();
    }


    public function posts()
    {
        return $this->belongsTo(WxPost::class,'id','user_id');
    }


    public function userFollow()
    {
        return $this->hasMany(WxUser::class,'user_follow_id','id');
    }

    /**
     * 关注数量
     * @param $uid
     * @return mixed
     */
    public function getFollowCountAttribute()
    {
        $uid = $this->id;
        return WxUserFollow::where('user_id',$uid)->count();
    }

    /**
     * 粉丝数量
     * @param $uid
     */
    public function getFollowUserCountAttribute()
    {
        $uid = $this->id;
        return WxUserFollow::where('user_follow_id',$uid)->count();
    }

    /**
     * 获赞👍数量
     * @param $uid
     * @return mixed
     */
    public function getLikeCountAttribute()
    {
        $uid = $this->id;
        $count1 = WxLike::where('posts_user_id',$uid)->count();
        $count2 = WxCommentLike::where('comment_user_id',$uid)->count();
        return $count1+$count2;
    }

//    public function setUserNameAttribute($value)
//    {
//        return base64_encode($value);
//    }
//
//    public function setWeixinNameAttribute($value)
//    {
//        return base64_encode($value);
//    }


//    public function getUserNameAttribute($value)
//    {
//        return base64_decode($value);
//    }
//
//    public function getWeixinNameAttribute($value)
//    {
//        return base64_decode($value);
//    }


}
